package com.jiangyg.mall.member.listener;

import com.jiangyg.mall.core.utils.Logger;
import com.jiangyg.mall.order.dto.OrderFinishDTO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 类描述：订单完成会员监听
 *
 * @author jiangyg
 * @date 2022-12-19
 */
@Slf4j
@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "order.finish.member.queue", autoDelete = "false"),
        exchange = @Exchange(value = "order-event-exchange", type = ExchangeTypes.TOPIC)
))
public class OrderFinishMemberQueueListener {

    @RabbitHandler
    public void listener(OrderFinishDTO dto, Channel channel, Message message) throws IOException {
        try {
            Logger.info(log, () -> String.format("[订单完成会员监听]-监听到订单[%d]已完成，开始会员积分处理……", dto.getId()));
            // TODO 释放订单使用的优惠卷
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception ex) {
            Logger.info(log, () -> String.format("[订单完成会员监听]-订单[%d]会员积分处理出错……", dto.getId()), ex);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
